package as3.main.pcxMenu 
{
	import Cain.initEffect.InitEffect;
	import com.greensock.TweenLite;
	import flash.display.Sprite;
	import flash.events.Event;
	import Cain.pcxbook.CainLoader;
	import flash.events.MouseEvent;
	/**
	 * 小照片選單
	 * @author cain in liontravel 2010
	 */
	public class PcxItem extends InitEffect 
	{
		static public const ITEM_CLICK:String = "PcxItem_ITEM_CLICK";
		
		public var pcx          :Sprite;
		public var pcxbg        :Sprite;
		public var cainLoader:CainLoader;
		private var id:int;
		public function PcxItem() 
		{
			setup();
		}
		/**
		 * 讀小圖
		 */
		public function pcx_url(_id:int,_pcx:String):void 
		{
			id = _id;
			cainLoader.load(_pcx);
		}
		/**
		 * 序號
		 */
		public function get pcx_id():int 
		{
			return id;
		}
		private function setup():void
		{
			cainLoader = new CainLoader(pcx.width, pcx.height);
			cainLoader.loadMovie = "LOADMV";
			pcx.addChild(cainLoader);
			addEventListener(Event.ADDED_TO_STAGE, onADDED_TO_STAGE);
		}
		private function onADDED_TO_STAGE(e:Event):void 
		{
			addEventListener(Event.REMOVED_FROM_STAGE, onREMOVED_FROM_STAGE);
			init();
		}
		private function init():void
		{
			addEventListeners();
		}
		
		private function addEventListeners():void
		{
			addEventListener(MouseEvent.ROLL_OVER, onROLL_OVER);
			addEventListener(MouseEvent.ROLL_OUT, onROLL_OUT);
			addEventListener(MouseEvent.CLICK, onCLICK);
		}
		
		private function onROLL_OVER(e:MouseEvent):void 
		{
			TweenLite.to(pcxbg, .5, { scaleX:1.2, scaleY:1.2 } );
		}
		
		private function onROLL_OUT(e:MouseEvent):void 
		{
			TweenLite.to(pcxbg, .5, { scaleX:1, scaleY:1 } );
		}
		
		private function onCLICK(e:MouseEvent):void 
		{
			dispatchEvent(new Event(ITEM_CLICK));
		}
		private function removeEventListeners():void
		{
			removeEventListener(MouseEvent.ROLL_OVER, onROLL_OVER);
			removeEventListener(MouseEvent.ROLL_OUT, onROLL_OUT);
			removeEventListener(MouseEvent.CLICK, onCLICK);
		}
		private function onREMOVED_FROM_STAGE(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onADDED_TO_STAGE);
			removeEventListener(Event.REMOVED_FROM_STAGE, onREMOVED_FROM_STAGE);
			removeEventListeners();
		}
	}
	
}